<?php

namespace app\api\controller;

use app\admin\model\DoMember;
use app\admin\model\Gupiao;
use app\admin\model\Guquan;
use app\admin\model\Projectfav;
use app\admin\model\Projectjoin;
use app\admin\model\Projectkefu;
use app\admin\model\Projects;
use app\admin\model\Projecttype;
use app\admin\model\Userkes;
use app\admin\model\Vip;
use app\admin\model\yalimedia\Teacher;
use app\common\controller\Api;
use think\Db;

class Info extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['kefu', 'test1','newquanyi'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function user()
    {
        $user = $this->auth->getUser();
        $user['is_vip'] = $user['level']>1?1:0;
        $this->success('成功',$user);
    }

    public function quanyi()
    {
        $map = [
            'user_id' => $this->auth->id,
        ];
        if(input('utype')){
            $map['utype'] = input('utype');
        }
        $data = Db::table('fa_userquanyi')->where($map)->select();
        $data = collection($data)->toArray();
        foreach($data as &$v){
            //$v['state_text'] = $v['state']== 1?'已分配':'待分配';
            $v['state_text'] = '';
            //$v['rate'] = $v['rate_text'];
            $v['rate'] = '';
            $v['image'] = cdnurl($v['image'],true);
            $v['area_text'] = '';
            if($v['area_id']){
                $name1 = '';
                $a1 = \app\admin\model\City::get($v['area_id']);
                $name2 = '';
                $name3 = '';
                $name4 = '';
                if($a1['pid']){
                    $name1 = $a1['name'];
                    $a2 = \app\admin\model\City::get($a1['pid']);
                    if($a2['pid']){
                        $name2 = $a2['name'];
                        $a3 = \app\admin\model\City::get($a2['pid']);
                        if($a3['pid']){
                            $name3 = $a3['name'];
                            $a4 = \app\admin\model\City::get($a3['pid']);
                            if($a4){
                                $name4 = $a4['name'];
                            }
                        }
                    }
                }
                $v['area_text'] = $name4.'-'.$name3.'-'.$name2.'-'.$name1;
            }
        }
        $this->success('成功', $data);
    }
    public function newquanyi()
    {
        $id = input('id');
        $p =Db::table('fa_userquanyi')->where('id',$id)->find();
        if(empty($p)){
            $this->error('参数错误');
        }
        $this->success('成功', $p);
    }
    public function quanyiDetail()
    {
        $id = input('id');
        $p = Guquan::get($id);
        if(empty($p)){
            $this->error('参数错误');
        }
        $data = [
            'image' =>'',
            'content' =>$p['content'],
        ];
        $this->success('成功', $data);
    }

    public function guquan()
    {
        $map = [
            'user_id' => $this->auth->id,
        ];
        $data = Db::table('fa_userguquan')->where($map)->select();
        $data = collection($data)->toArray();
        foreach($data as &$v){
            $g = Guquan::get($v['guquan_id']);
            $g['id'] = $g?$g['id']:'';
            $v['title'] = $g?$g['title']:'';
            $v['state_text'] = $v['rate_text'];
            $v['user'] =  '持有者 ';
        }
        $this->success('成功', $data);
    }
    public function gupiao()
    {
        $map = [
            'user_id' => $this->auth->id,
        ];
        $data = Db::table('fa_usergupiao')->where($map)->select();
        $data = collection($data)->toArray();
        foreach($data as &$v){
            $g = Gupiao::get($v['guquan_id']);
            $g['id'] = $g?$g['id']:'';
            $v['title'] = $g?$g['title']:'';
            $v['state_text'] = $v['rate_text'];
        }
        $this->success('成功', $data);
    }

    public function gupaioDetail()
    {
        $id = input('id');
        $p = Gupiao::get($id);
        if(empty($p)){
            $this->error('参数错误');
        }
        $img = explode(',',$p['images']);
        foreach ($img as &$v){
            $v = cdnurl($v,true);
        }
        $p['images'] = $img;
        $this->success('成功', $p);
    }

    public function mygupiao()
    {
        $map = [
            'user_id' => $this->auth->id,
        ];
        $data = Db::table('fa_project_join')->where($map)->select();
        $data = collection($data)->toArray();
        foreach($data as &$v){
            $g = Projects::get($v['project_id']);
            $v['id'] = $g?$g['id']:'';
            $v['title'] = $g?$g['project_name']:'';
            $list = Projecttype::where('id','in',explode(',',$v['projecttype_ids']))->select();
            $txt = '';
            if($list){
                $l = array_column($list,'typename');
                $txt = implode(',',$l);
            }
            $v['state_text'] = $txt;
        }
        $this->success('成功', $data);
    }

    public function kefu()
    {
        $list = Projectkefu::where('state',1)->where('is_set',1)->limit(2)->select();
        /*$config = config('site.kefu');
        if(is_array($config)){
            $list = [];
            foreach($config as $k=>$v){
                $list[$k]['mobile'] = $v;
            }
        }*/
        foreach($list as &$v){

            $v['image'] = cdnurl($v['image'],true);
            $v['qrimage'] = cdnurl($v['qrimage'],true);

        }
        $this->success('成功',$list);
    }
    //我的课程
    public function course()
    {
        $map = [
            'pay_status' => 1,
            'user_id' => $this->auth->id,
        ];
        //$map = "";
        $list = Db::table('fa_yalimedia_order')
            ->where($map)
            ->field("id,item_id,user_id")
            ->paginate(input('limit',15))->toArray();
        foreach($list['data'] as &$v){
            $c = Db::table('fa_yalimedia_course')
                ->where('id',$v['item_id'])
                ->field('id as course_id,title,cover,content')
                ->find();
            if($c){
                $c['cover'] = cdnurl($c['cover']);
            }
            $v['course'] = $c;
        }
        $this->success('成功',$list);
    }

    public function mydaili()
    {
        $v1  = Vip::get(1);
        $v2  = Vip::get(2);
        $data = [
            'daili' => [
                'daili_city' => '深圳市',
                'daili_city' => '',
                'daili_level'=>'城市代理',
                'daili_level'=>'',
                'desc' => $v1['content'],
            ],
            'jingji'=>[
                'list' =>[
                   /* ['name' => '张三','name_text' => '我是经纪人'],
                    ['name' => '李四','name_text' => '我是经纪人'],
                    ['name' => '王五','name_text' => '我是经纪人'],*/
                ],
                'desc' => $v2['content'],
            ],
        ];
        $this->success('成功',$data);
    }

    public function dailipro()
    {
        $map = [
            'user_id' => $this->auth->id,
        ];
        $data = Projectjoin::where($map)->paginate()->toArray();
        foreach($data['data'] as &$v){
            $pro = Projects::get($v['project_id']);
            $v['project_name'] = $pro?$pro['project_name']:'';
            $v['image'] = $pro?cdnurl($pro['image'],true):'';
            $v['project_desc'] = $pro?strip_tags($pro['project_desc']):'';
        }
        $this->success('成功',$data);
    }

    public function myteach()
    {
        $where = [
            "user_id" => $this->auth->id,
        ];
        $where = "";
        $teach = DoMember::where($where)
            ->where('status',1)
            ->where('teacher_id','neq',0)
            ->field('id,teacher_id')
            ->select();
        $ids = array_column($teach,'teacher_id');
        $list = Teacher::where("id",'in',$ids)->paginate()->toArray();
        foreach($list['data'] as &$v){
            $v['image'] = cdnurl($v['image'],true);
        }
        $this->success('成功', $list);
    }

    public function myshop()
    {
        $v1  = Vip::get(6);
        $v2  = Vip::get(5);
        $data = [
            'chanye' => [
                'desc' => $v1['content'],
            ],
            'proejct'=>[
                'desc' => $v2['content'],
            ],
        ];
        $this->success('成功',$data);
    }

    public function myteam()
    {
        $data = [
            ['id' => 1,'mobile'=>'138123456','name' => '陈萌萌1','avatar' =>'https://d-chanxiangjc.bigchun.com//chanxiang/uploads/20240411/9cff533805f6a666f7eebdd48ec2323a.png','area_text' =>'广东 深圳'],
            ['id' => 2,'mobile'=>'138123456','name' => '陈萌萌2','avatar' =>'https://d-chanxiangjc.bigchun.com//chanxiang/uploads/20240411/9cff533805f6a666f7eebdd48ec2323a.png','area_text' =>'广东 深圳'],
            ['id' => 3,'mobile'=>'138123456','name' => '陈萌萌3','avatar' =>'https://d-chanxiangjc.bigchun.com//chanxiang/uploads/20240411/9cff533805f6a666f7eebdd48ec2323a.png','area_text' =>'广东 深圳'],
            ['id' => 4,'mobile'=>'138123456','name' => '陈萌萌4','avatar' =>'https://d-chanxiangjc.bigchun.com//chanxiang/uploads/20240411/9cff533805f6a666f7eebdd48ec2323a.png','area_text' =>'广东 深圳'],
            ['id' => 5,'mobile'=>'138123456','name' => '陈萌萌5','avatar' =>'https://d-chanxiangjc.bigchun.com//chanxiang/uploads/20240411/9cff533805f6a666f7eebdd48ec2323a.png','area_text' =>'广东 深圳'],

        ];
        $data = [];
        $this->success('成功',$data);
    }
    //项目收藏
    public function profav()
    {
        if(empty(input('project_id'))){
            $this->error('project_id必传');
        }
        $map =[
            'project_id' => input('project_id'),
            'user_id' => $this->auth->id,
        ];
        $p = Projectfav::where($map)->find();
        if($p){
            Projectfav::where($map)->delete();
            $this->success('取消收藏');
        }
        $data = input();
        $data['user_id'] = $this->auth->id;
        $model = new Projectfav($data);
        $model->allowField(true)->save();
        $this->success('收藏成功');
    }

    //课程收藏
    public function fav(){
        if(empty(input('item_id'))){
            $this->error('item_id必传');
        }
        $map =[
            'item_id' => input('item_id'),
            'user_id' => $this->auth->id,
        ];
        $p = Db::table('fa_yalimedia_fav')->where($map)->find();
        if($p){
            Db::table('fa_yalimedia_fav')->where($map)->delete();
            $this->success('取消收藏');
        }
        $data['item_id'] = input('item_id');
        $data['user_id'] = $this->auth->id;
        $data['createtime'] =time();
        Db::table('fa_yalimedia_fav')->insert($data);
        $this->success('收藏成功');
    }

    public function mycourse()
    {
        $map =[
            'user_id' => $this->auth->id,
        ];
        //$map ="";
        $list = Db::table('fa_yalimedia_fav')->where($map)->paginate()->toArray();
        foreach($list['data'] as &$v){
            $c = Db::table('fa_yalimedia_course')
                ->where('id',$v['item_id'])
                ->field('id as course_id,title,cover,content')
                ->find();
            if($c){
                $c['cover'] = cdnurl($c['cover']);
            }
            $v['course'] = $c;
            $v['is_buy'] = 0;//1=已购买，0=未购买
        }
        $this->success('成功',$list);
    }

    public function favproject()
    {
        $map =[
            'user_id' => $this->auth->id,
        ];
        //$map = "";
        $list = Projectfav::where($map)->paginate()->toArray();
        foreach($list['data'] as &$v){
            $c = Db::table('fa_project')
                ->where('id',$v['project_id'])
                ->field('id as proejct_id,project_name,image,project_desc')
                ->find();
            if($c){
                $c['image'] = cdnurl($c['image']);
            }
            $v['fav_num'] = 1;
            $v['project'] = $c;
        }
        $this->success('成功',$list);
    }

    public function mykehu()
    {
        $map =[
            'user_id' => $this->auth->id,
        ];
        if(input('search')){
            $map['name'] = ['like','%'.input('search').'%'];
        }
        //$map = "";
        $list = Userkes::where($map)->paginate()->toArray();
        foreach($list['data'] as &$v){
            if(empty($v['image'])){
                $v['image'] = request()->domain().'/assets/img/avatar.png';
            }else{
                $v['image'] = cdnurl($v['image'],true);
            }
        }
        $this->success('成功',$list);
    }
}